package com.miyake.client.datamanage;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DataViewer extends Composite {
//	private List<NodeUpdateHandler> handlers = new ArrayList<NodeUpdateHandler>();
//	public void addDataViewerHandler(NodeUpdateHandler handler) {
//		handlers.add(handler);
//	}
//	private void fireNodeUpdateEvent(String key) {
//		for (NodeUpdateHandler handler : handlers) {
//			handler.onNodeUpdate(key);
//		}
//	}
	
	private VerticalPanel verticalPanel;
	private ToolBarHandler createHandler;
	private Page page;
	private NodeUpdateHandler updateHandler = null;
	private boolean createEnabled = false;
	private boolean updateEnabled = false;
	private boolean guestOperationEnabled = false;
	private GuestHandler guestHandler;
	
	public DataViewer() {
		initialize(null);
	}
	
	private void initialize(ToolBarHandler handler) {
		this.createHandler = handler;
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
	}

	public void setSelectedNode(NodeProperty node) {
/*
		NodeUpdateHandler updateHandler = null;
		
		if (canEdit()) {
			
			updateHandler = new NodeUpdateHandler() {
				@Override
				public void onNodeUpdate(String key) {
					fireNodeUpdateEvent(key);
				}
			};
		}
*/		
		Page newPage = null;
		if (node.getType() == NodeProperty.DATA) {
			newPage = new DataPage(node, verticalPanel);
		}
		else if (node.getType() == NodeProperty.PLOT) {
			newPage = new PlotPage(node, verticalPanel);
		}
		else if (node.getType().equals(NodeProperty.FOLDER)) {
			newPage = new FolderPage(node, verticalPanel);
		}
		else if (node.getType().equals(NodeProperty.ROOTNODE)) {
			newPage = new FolderPage(node, verticalPanel);
		}
		else if (node.getType().equals(NodeProperty.TEXT)) {
			page.setSelected(node);
		}
		else if (node.getType().equals(NodeProperty.TABLE)) {
			newPage = new TablePage(node, verticalPanel);
		}
		else if (node.getType().equals(NodeProperty.IMAGE)) {
			newPage = new ImagePage(node, verticalPanel);
		}
		else if (node.getType().equals(NodeProperty.PAPER)) {
			newPage = new PaperPage(node, verticalPanel);
		}
		else if (node.getType().equals(NodeProperty.ALIAS)) {
			page.setSelected(node);
		}
		
		if (newPage != null && newPage != page) {
			newPage.addCreateHandler(createHandler);
			newPage.addNodeUpdateHandler(updateHandler);
//			newPage.addGuestHandler(guestHandler);
			page = newPage;
			page.update();
		}
		
	}

	private boolean canEdit() {
		if (createHandler != null) {
			return true;
		}
		return false;
	}

	public void addNodeUpdateHandler(NodeUpdateHandler updateHandler) {
		this.updateHandler  = updateHandler;
	}

	public void addCreateHandler(ToolBarHandler createHandler) {
		this.createHandler = createHandler;
	}
	
	public void addGuestOperationHandler(GuestHandler guestHandler) {
		this.guestHandler = guestHandler;
	}
	public boolean isCreateEnabled() {
		return createEnabled;
	}

	public void setCreateEnabled(boolean createEnabled) {
		this.createEnabled = createEnabled;
	}

	public boolean isUpdateEnabled() {
		return updateEnabled;
	}

	public void setUpdateEnabled(boolean updateEnabled) {
		this.updateEnabled = updateEnabled;
	}

	public boolean isGuestOperationEnabled() {
		return guestOperationEnabled;
	}

	public void setGuestOperationEnabled(boolean guestOperationEnabled) {
		this.guestOperationEnabled = guestOperationEnabled;
	}
	
	
}
